﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Web;
using EmoneyWeiXinCommon;

namespace EmoneyWeiXinDAL
{
    public class PageHelper
    {
        string procName = "proc_Pagination";//分页存储过程的名称


        public string TableName
        {
            get;
            set;
        }

        public string KeyName
        {
            get;
            set;
        }

        public int PageSize
        {
            get;
            set;
        }

        public string Filter
        {
            get;
            set;
        }

        public string WhereText
        {
            get;
            set;
        }

        public string Orderby
        {
            get;
            set;
        }

        public int CountPage
        {
            get;
            private set;
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecord
        {
            get;
            private set;
        }

        public int CurrentPage
        {
            get;
            set;
        }


        public PageHelper(string tableName, string keyName, int pageSize = 10, int currentPage = 1, string filter = "*", string whereText = "", string orderBy = "")
        {
            this.TableName = tableName;
            this.KeyName = keyName;
            this.PageSize = pageSize;
            this.CurrentPage = currentPage;
            this.Orderby = orderBy;
            this.Filter = filter;
            this.WhereText = whereText;
        }

       

        public DataTable GetData()
        {
            List<SqlParameter> lstPara = new List<SqlParameter>();
            lstPara.Add(new SqlParameter("@tableName", TableName));
            lstPara.Add(new SqlParameter("@keyName", KeyName));
            lstPara.Add(new SqlParameter("@pageSize", PageSize));
            lstPara.Add(new SqlParameter("@pageIndex", CurrentPage));
            lstPara.Add(new SqlParameter("@Filter", Filter));
            lstPara.Add(new SqlParameter("@whereText", WhereText));
            lstPara.Add(new SqlParameter("@orderby", Orderby));
            SqlParameter outP = new SqlParameter("@DataCount", SqlDbType.Int);
            outP.Direction = ParameterDirection.Output;
            lstPara.Add(outP);
            DataTable dt = GetDataTable(procName, lstPara, CommandType.StoredProcedure);
            TotalRecord = Convert.ToInt32(outP.Value);
            CountPage = (TotalRecord - 1) / PageSize + 1;
            return dt;
        }

        public List<T> GetDataForModelList<T>()
        {
            return new List<T>();
        }

        private DataTable GetDataTable(string sql, List<SqlParameter> paras, CommandType compandtype)
        {
            return SQLHelper.ExecuteGetTable(Config.EmoneyWeiXin, compandtype, sql, paras.ToArray());
        }
    }
}
